const { articleTable, likesIpTable, readingTimesIpTable } = require('../../schemas/index');

const { getResJson } = require('../../assets/util');

/**
 * 查询文章详情
 * @param {Object} params 参数
 * @param {Function} callback 回调函数
 */
exports.getArticleDetail = (params, callback) => {
    let ipInfo = readingTimesIpTable.findOne({
        articleId: params.articleId,
        ip: params.ip
    })
    ipInfo.exec((err, ip) => {
        // 点赞时阅读次数不+1
        // 每个ip一天多次访问同一篇文章，阅读次数只加1
        let num = (params.likes || ip) ? 0 : 1;
        let result = articleTable.updateOne({ _id: params.articleId }, {
            $inc: {
                readingTimes: num
            }
        });
        result.exec((err, rs) => {
            articleTable.findOne({ _id: params.articleId }, (err, data) => {
                let resJson = {};
                if (err) {
                    resJson = getResJson(data, '查询失败', false);
                } else {
                    if (!ip) {
                        readingTimesIpTable.create(params, err => {
                            if (err) {
                                console.log('---------');
                                console.log('阅读次数 - ip插入失败');
                                console.log(err);
                            }
                        })
                    }
                    resJson = getResJson(data, '查询成功');
                }
                callback(resJson);
            })
        })
    })
}

/**
 * 根据首页中不同的 模块 查询文章
 * @param {Object} params 参数
 * @param {Function} callback 回调函数
 */
exports.searchArticle = (params, callback) => {
    let type = params.type ? params.type : '';
    let limit = 0;
    let search = {};
    let sort = {
        compareDate: -1
    };
    switch (type) {
        case 'isSpecialRecommend': // 首页特别推荐
            limit = 9;
        case 'isTop': // 首页banner
            limit = 3;
            search = {
                [type]: true
            }
            break;
        case 'readingTimes': // 首页点击排行
            sort = {
                readingTimes: -1
            };
            break;
        default: // 首页最新文章
            limit = 10;
            search = {
                isSpecialRecommend: false,
                isTop: false
            };
            break;
    }
    articleTable.find(search, (err, data) => {
        let resJson = {};
        if (err) {
            resJson = getResJson(data, '查询失败', false);
        } else {
            resJson = getResJson(data, '查询成功');
        }
        callback(resJson);
    }).limit(limit).sort(sort)
}

/**
 * 查询不同类型的文章 or 不同类型下点击次数前十的文章
 * @param {Object} params 参数
 * @param {Function} callback 回调函数
 */
exports.searchArticleForType = (params, callback) => {
    let sort = {
        compareDate: -1
    }
    if (params.readingTimes) {
        sort = {
            readingTimes: -1
        };
    }
    let count = articleTable.count({ type: params.type });
    count.exec((err, rs) => {
        articleTable.find({ type: params.type }, (err, data) => {
            let resJson = {};
            if (err) {
                resJson = getResJson(data, '查询失败', false);
            } else {
                let paramsJson = {
                    list: data,
                    total: rs
                }
                resJson = getResJson(paramsJson, '查询成功');
            }
            callback(resJson);
        }).limit(params.pageSize).skip((params.pageNum - 1) * params.pageSize).sort(sort)
    })
}

/**
 * 文章点赞
 * @param {Object} params 参数
 * @param {Function} callback 回调函数 
 */
exports.articlesLikes = (params, callback) => {
    let ipInfo = likesIpTable.findOne({
        articleId: params.articleId,
        ip: params.ip
    })
    // 每个ip一天只能给一篇文章点一次赞
    ipInfo.exec((err, rs) => {
        if (rs) {
            callback(getResJson({}, '您今天已给该文章点赞！', false));
            return;
        }
        articleTable.updateOne({ _id: params.articleId }, {
            $inc: {
                likes: 1
            }
        }, (err, data) => {
            let resJson = {};
            if (err) {
                resJson = getResJson(data, '点赞失败', false);
            } else {
                likesIpTable.create(params, err => {
                    if (err) {
                        console.log('---------');
                        console.log('点赞次数 - ip插入失败');
                        console.log(err);
                    }
                })
                resJson = getResJson(data, '点赞成功');
            }
            callback(resJson);
        })
    })
}